import { areaList } from '@vant/area-data';
const Multipart = require('../../utils/Multipart.min') 
Page({
  data: {
    areaList,
    fileList:[],
    ageList: ['1个月','1-3个月','3-6个月','6-9个月','9-12个月','1岁','2岁','3岁','3岁+'],
    isShow:false,
    showPosition:false,
    info:{
      userId:"",
      type:0,
      spe:0,
      var:"",
      gender:0,
      city:"",
      number:"",
      wxCode:"",
      desc:""
    }
  },
  afterRead(e:any){
    const { file } = e.detail
    const fileList = this.data.fileList as any
    file.forEach((item:{url:string})=>{
      fileList.push(item)
    })
   this.setData({
      fileList
   })
  },
  // 寻宠 || 寻主
  changeType(e:any){
    let type = +e.target.dataset.type
    this.setData({
      "info.type":type
    })
  },
  // 星球
  speChange(e:any){
    this.setData({
      "info.spe":+e.detail
    })
  },
  //性别
  genderChange(e:any){
    this.setData({
      "info.gender":+e.detail
    })
  },
  selectPosition(){
    this.setData({
      showPosition:true
    })
  },
  positionClose(){
    this.setData({
      showPosition:false
    })
  },
  positionConfirm(e:any){
    let position = ""
    e.detail.values.forEach((item:any)=>{
      position = position + ' ' + item.name
    })
    this.setData({
      "info.city":position,
      showPosition:false
    })
  },
  positionCancel(){
    this.setData({
      showPosition:false
    })
  },
  infoChange(e:any){
    let name = e.currentTarget.dataset.name
    if(name=="var"){
      this.setData({
        "info.var":e.detail
      })
    }
    if(name=="number"){
      this.setData({
        "info.number":e.detail
      })
    }
    if(name=="wxCode"){
      this.setData({
        "info.wxCode":e.detail
      })
    }
  },
  descChange(e:any){
    this.setData({
      "info.desc":e.detail.value
    })
  },
  async upload(){
    //获取文件
   const fileList = this.data.fileList
   let files = fileList.map((item:any)=>({name:'photo',filePath:item.url}))
   let info = this.data.info  as any
   console.log(info);

   let isNotEmpt = Object.keys(info).every(k =>{
      return  String(info[k]) != ""
   }) 
   if(isNotEmpt){
     let fields = Object.keys(info).map(key => ({
       name: key,
       value: info[key]
     }))
     try {
       let result = await new Multipart({files, fields})
       .submit('http://localhost:9000/v1/serch')
       wx.showToast({
         title:"上传成功",
         icon:"success"
       })
       wx.navigateTo({url:"/pages/xunchong/xunchong"})
     } catch (error) {
       wx.showToast({
         title:"上传失败",
         icon:"error" as any
       })
     }
   }else{
     wx.showToast({
       title:"信息不完整",
       icon:"error" as any
     })
   }
 },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {
    const {id} = wx.getStorageSync("userInfo")
    this.setData({
      "info.userId":id
    })
  }
})